import * as YvanUI from "yvan-ui-wotu";

const _ = YvanUI.lodash

const React = YvanUI.React

interface InP {
    isNew: boolean;
    rowData?: any;
    callBack?: ()=>void;
}

interface Ref {
    theform: YvanUI.Form
    tasks: YvanUI.Repeat
}

export default class Dialog extends YvanUI.Scope<InP, Ref> {
    main = this.$$({
        front_class: [],
        repeat: '',
        links: []
    })

    onRender() {
        // @ts-ignore
        YvanUI.ajax({url: "/_yvanui/query", method: 'post-json', data:
                {
                    params: { front_class: this.inParamter.rowData.front_class },
                    db: 'master',
                    limit: -1,
                    limitOffset: 0,
                    needCount: false,
                    sqlId: 'correlation_byFront',
                    moduleId: 'o2o'
                }
        }).then(res=>{
            _.forEach(res.data.data, (d)=> {
                const correlation_class = d.correlation_class;
                this.main.links.push({correlation_class: window['correlationMap'][correlation_class].idArr});
            });

        }).catch(reason => {

        }).finally(()=>{

        })
        this.main.front_class = window['correlationMap'][this.inParamter.rowData.front_class].idArr
    }

    vjson = {
        view: 'page',
        title: this.inParamter.isNew?"添加":"编辑" + "三级分类关联",
        items: [
            {
                view: 'form',
                ref: 'theform',
                items: [
                    {
                        label: '前端三级分类',
                        view: 'treeSelect',
                        bind: 'main.front_class',
                        width: '600px',
                        idField: 'id',
                        labelField: 'text',
                        // parentField: 'parent_id',
                        childrenField: 'items',
                        data: window['dict'].cats,
                        dataType: 'tree'
                    },
                    {
                        view:'divider',
                        title: '',
                    },
                    {
                        view: 'repeat',
                        label: '关联三级分类',
                        bind: 'main.links',
                        ref: 'tasks',
                        allowRemove: true,
                        loading: false,
                        items: [
                            {
                                label: '三级分类',
                                view: 'treeSelect',
                                width: '500px',
                                idField: 'id',
                                labelField: 'text',
                                childrenField: 'items',
                                bindChild: 'correlation_class',
                                data: window['dict'].cats,
                                dataType: 'tree'
                            }
                        ]
                    },
                ]
            }
        ],
        footer: [
            {
                view: 'button',
                text: '提交',
                onClick: ()=>{
                    if (this.main.front_class.length<=0) {
                        YvanUI.showError("请添加关联！")
                        return
                    }

                    const me = this;

                    const ids = [];
                    _.forEach(this.main.links, ({correlation_class})=>{
                        if (_.isArray(correlation_class) && correlation_class.length>0) {
                            ids.push(correlation_class[correlation_class.length-1])
                        }
                    })

                    if (ids.length<=0) {
                        YvanUI.showError("请添加关联！")
                        return;
                    }

                    // @ts-ignore
                    YvanUI.ajax({url: "/_yvanui/execute", method: 'post-json', data:
                            {
                                params: {front_class: this.main.front_class[this.main.front_class.length-1], correlation_class: ids },
                                db: 'master',
                                sqlId: 'correlation_save',
                                moduleId: 'o2o'
                            }
                    }).then(res=>{
                        YvanUI.showInfo('成功');
                        me.close()
                        if (typeof me.inParamter.callBack === "function") {
                            me.inParamter.callBack()
                        }
                    })
                }
            }
        ]

    }
}